package com.yanqu.road.dao.impl.activity.tongbing;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingBattlefieldRank;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingCityWarReport;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingUserWarReport;

import java.sql.*;
import java.util.*;

public class CrossTongBingReportDaoImpl extends TempDao {


    private CrossTongBingCityWarReport initCityReportData(ResultSet rs) throws SQLException{
        CrossTongBingCityWarReport data = new CrossTongBingCityWarReport();
        data.setActivityId(rs.getInt("activityId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setAtkCampId(JSON.parseObject(rs.getString("atkCampId"), new TypeReference<List<Integer>>() {}));
        data.setDefCampId(rs.getInt("defCampId"));
        data.setType(rs.getInt("type"));
        data.setCityId(rs.getInt("cityId"));
        data.setAtkRankList(JSON.parseObject(rs.getString("atkRankList"), new TypeReference<LinkedList<CrossTongBingBattlefieldRank>>() {}));
        data.setDefRankList(JSON.parseObject(rs.getString("defRankList"), new TypeReference<LinkedList<CrossTongBingBattlefieldRank>>() {}));
        data.setWinCampId(rs.getInt("winCampId"));
        data.setTime(rs.getLong("time"));
        return data;
    }

    public List<CrossTongBingCityWarReport> getCityReportList(int activityId, int groupId) {
        List<CrossTongBingCityWarReport> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_tongbing_city_report WHERE activityId = ? and groupId = ? ORDER BY `time` ASC";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossTongBingCityWarReport data = initCityReportData(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }


    public boolean addCityReportList(List<CrossTongBingCityWarReport> reportList) {
        boolean result = false;
        String sql = "INSERT INTO t_c_tongbing_city_report(activityId,groupId,atkCampId,defCampId,type,cityId,atkRankList,defRankList,winCampId,time) " +
                "VALUES(?,?,?,?,?,?,?,?,?,?)";
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (CrossTongBingCityWarReport data : reportList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAtkCampId())));
            para.put(i++, new DbParameter(Types.INTEGER, data.getDefCampId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getCityId()));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAtkRankList())));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDefRankList())));
            para.put(i++, new DbParameter(Types.INTEGER, data.getWinCampId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getTime()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    private CrossTongBingUserWarReport initUserReportData(ResultSet rs) throws SQLException{
        CrossTongBingUserWarReport data = new CrossTongBingUserWarReport();
        data.setActivityId(rs.getInt("activityId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setNo(rs.getInt("no"));
        data.setUserIdDef(rs.getLong("userIdDef"));
        data.setUserIdAct(rs.getLong("userIdAct"));
        data.setDefPatronsId(rs.getInt("defPatronsId"));
        data.setActPatronsId(rs.getInt("actPatronsId"));
        data.setDefSoldierNum(rs.getLong("defSoldierNum"));
        data.setActSoldierNum(rs.getLong("actSoldierNum"));
        data.setDefAddition(rs.getInt("defAddition"));
        data.setActAddition(rs.getInt("actAddition"));
        data.setDefZhanGong(rs.getLong("defZhanGong"));
        data.setActZhanGong(rs.getLong("actZhanGong"));
        data.setDefDieSoldierNum(rs.getLong("defDieSoldierNum"));
        data.setActDieSoldierNum(rs.getLong("actDieSoldierNum"));
        data.setWinUserId(rs.getLong("winUserId"));
        data.setTime(rs.getDate("time").getTime());
        data.setCityId(rs.getInt("cityId"));
        return data;
    }

    public List<CrossTongBingUserWarReport> getUserReportList(int activityId, int groupId) {
        List<CrossTongBingUserWarReport> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_tongbing_user_report WHERE activityId = ?  and groupId=? ORDER BY `time` ASC";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossTongBingUserWarReport data = initUserReportData(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }


    public boolean addUserReportList(List<CrossTongBingUserWarReport> reportList) {
        boolean result = false;
        String sql = "INSERT INTO t_c_tongbing_user_report(activityId,groupId,no,userIdDef,userIdAct,defPatronsId,actPatronsId,defSoldierNum,actSoldierNum,defAddition,actAddition,defZhanGong,actZhanGong,defDieSoldierNum,actDieSoldierNum,winUserId,time,cityId) " +
                "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (CrossTongBingUserWarReport data : reportList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getNo()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserIdDef()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserIdAct()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getDefPatronsId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getActPatronsId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getDefSoldierNum()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getActSoldierNum()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getDefAddition()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getActAddition()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getDefZhanGong()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getActZhanGong()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getDefDieSoldierNum()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getActDieSoldierNum()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getWinUserId()));
            para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(data.getTime())));
            para.put(i++, new DbParameter(Types.INTEGER, data.getCityId()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

}
